// 导入db模块
const db = require("../db");

exports.deviceLogTop20 = async (req, res) => {
    try {
        const { device_id, startTime } = req.body
        if (!device_id) return res.sendData({ message: "设备ID不能为空" }, 500)
        if (!startTime) return res.sendData({ message: "开始时间不能为空" }, 500)
        const [rows, crawl, marks, battery] = await Promise.all([
            db.query("SELECT * FROM device_log WHERE device_id = ? AND create_time >= ? ORDER BY create_time DESC", [device_id, startTime]),
            db.query("SELECT * FROM device_signal WHERE create_time >= ? AND device_id = ? AND command = '2' ORDER BY create_time DESC LIMIT 1", [startTime, device_id]),
            db.query("SELECT * FROM device_signal WHERE create_time >= ? AND device_id = ? AND command = '1' ORDER BY create_time DESC", [startTime, device_id]),
            db.query("SELECT * FROM device_battery WHERE device_id = ? AND create_time >= DATE_SUB(NOW(), INTERVAL 30 MINUTE) ORDER BY create_time DESC LIMIT 1", [device_id])
        ]);
        res.sendData({ message: "查询成功!", data: rows, marks: marks, battery: battery, crawl: crawl});
    } catch (err) {
        res.sendData({ message: "Failed to execute query:" + err }, 500);
    }
};